package com.jj.lib;

import android.Manifest;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.jj.base.BaseActivity;
import com.jj.network.okhttp.BaseCallback;
import com.jj.util.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

import okhttp3.Response;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Button btn1, btn2, btn3, btn4, btn5, btn6;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);

        setSwipeBackEnable(false);
    }

    @Override
    protected void initData() {
        showPermissionsResult(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE)
            showPermissionsResult(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected boolean isToolBarBack() {
        return false;
    }

    @Override
    protected boolean isToolBarTitle() {
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(MainActivity.this, CActvivty.class);
            startActivity(intent);
            return true;
        }
        if (id == R.id.action) {
            Intent intent = new Intent(MainActivity.this, DActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                showSnackbar(findViewById(android.R.id.content), "点击我知道我才能打开", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, BActvivty.class);
                        startActivity(intent);
                    }
                });
                break;
            case R.id.btn2:
                Intent intent2 = new Intent(MainActivity.this, CActvivty.class);
                startActivity(intent2);
                break;
            case R.id.btn3:
                Intent intent3 = new Intent(MainActivity.this, DActivity.class);
                startActivity(intent3);
                break;
            case R.id.btn4:
                Intent intent4 = new Intent(MainActivity.this, EActivity.class);
                startActivity(intent4);
                break;
            case R.id.btn5:
                showWaitDialog();
                showSnackbar("按返回键取消");
                showCustomTopToast("这世间的哈及单号发放啊，实打实分地方大幅度发");
                break;
            case R.id.btn6:
                showWaitDialog();
                OkHttpUtils.API.getDriverexam(10, 1, new ArrayBack(this));
//                OkHttpUtils.API.postAPIs("", new DownloadCallback(MainActivity.this) {
//
//                    @Override
//                    protected void onSuccess(String result) {
//
//                    }
//
//                    @Override
//                    protected void onError(String error) {
//
//                    }
//
//                    @Override
//                    protected void onEnd() {
//
//                    }
//                });
                break;
        }
    }

    private static class ArrayBack extends BaseCallback<ArrayList<ExamModel>> {
        private WeakReference<MainActivity> mWeakReference;

        ArrayBack(MainActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }

        @Override
        protected ArrayList<ExamModel> parsingData(Response response) {
            ArrayList<ExamModel> list = new ArrayList<>();
            try {
                ExamModel mo = new ExamModel();
                String str = response.body().string();
                LogUtils.dc("EXT", str);
                JSONObject json = new JSONObject(str);
                mo.setStatus(json.getInt("status"));
                mo.setMag(json.getString("msg"));
                mo.setResult(json.getString("result"));
                list.add(mo);
            } catch (JSONException | IOException e) {
                e.printStackTrace();
            }
            return list;
        }

        @Override
        protected void onSuccess(ArrayList<ExamModel> result) {
            mWeakReference.get().showCustomTopToast(result.get(0).getMag());
        }


        @Override
        protected void onError(String error) {

        }

        @Override
        protected void onEnd() {

        }
    }

}
